//表格配置
var dataTableconfig ={
		"processing" : true,
		"serverSide" : true,
		"paging" : true,
		"lengthChange" : false,
		"searching" : false,
		"ordering" : false,
		"info" : true,
		"autoWidth" : false,
		"oLanguage" : {
			"sProcessing" : "正在加载中......",
			"sLengthMenu" : "每页显示 _MENU_ 条记录",
			"sZeroRecords" : "对不起，查询不到相关数据！",
			"sEmptyTable" : "表中无数据存在！",
			"sInfo" : "当前显示 _START_ 到 _END_ 条，共 _TOTAL_ 条记录",
			"sInfoFiltered" : "数据表中共为 _MAX_ 条记录",
			"sSearch" : "搜索",
			"oPaginate" : {
				"sFirst" : "首页",
				"sPrevious" : "上一页",
				"sNext" : "下一页",
				"sLast" : "末页"
			}
		},
		"oTableTools" : {
			"sRowSelect" : "multi",
			"aButtons" : [
					{
						"sExtends" : "new_record",
						"sButtonText" : "Add"
					},
					{
						"sExtends" : "select",
						"sButtonText" : "Delete Recods",
						"fnClick" : function(nButton,
								oConfig, oFlash) {
							//delete stuff comes here 
							alert('test');
						}

					} ]
		},
		ajax : "/menu/queryMenu",
		columns : [
				{
					data : "MENU_ID",
					render : function(data, type, full) {
						return '<input name=\"items\" type="checkbox" id=\"check_'+data+'\" value="' + data + '" name="subBox" />';
					}
				}, {
					"data" : "MENU_ID"
				},{
					"data" : "PARENT_ID"
				}, {
					"data" : "PARENT_NAME"
				}, {
					"data" : "CODE"
				}, {
					"data" : "NAME"
				},{
					"data": "URL",
				},{
					"data":"SEQ",
				},{
					"data":"CSSCLASS",
				}],
		columnDefs: [    
		        {    
		        	"targets": [2], //隐藏第二列，从第0列开始   
		        	"visible": false    
		        }], 
		"dom" : "<'row'<'col-xs-2'l><'#mytool.col-xs-4'><'col-xs-6'f>r>"
				+ "t"
				+ "<'row'<'col-xs-6'i><'col-xs-6'p>>",

	};

//初始化表格
var table = $('#menu_table').DataTable(dataTableconfig);


$(function() {
			//全选
			$("#checkall").click(function() {
				$('[name=items]:checkbox').prop("checked", this.checked);
			});
			$('[name=items]:checkbox').click(
					function() {
						var $tmp = $('[name=items]:checkbox');
						$('#checkall').prop('checked',
								$tmp.length == $tmp.filter(':checked').length);
					});

			//输出值
			$("#send").click(function() {
				var str = "你选中的是：\r\n";
				$('[name=items]:checkbox:checked').each(function() {
					str += $(this).val() + "\r\n";
				})
				alert(str);
			});
		});



var app = angular.module('menuApp', []);
//初始化导航栏
var initController = app
		.controller(
				"menuController",
				function($scope, $http) {
					$('#myModal').on('hide.bs.modal', function () {
						document.getElementById("addForm").reset(); 
					});
					
					$http({
						url : '/menu/queryParentList',
						method : 'GET'
					}).success(function(data, header, config, status) {
						// 响应成功
						$scope.parentList = data;
					}).error(function(data, header, config, status) {
						// 处理响应失败
					});
					$scope.add = function(){
						var form = document.getElementById("addForm");
						var code = form.code.value;
						var name = form.name.value;
						var parent_id = form.parent_id.value;
						var type = form.type.value;
						var url = form.url.value;
						var seq = form.seq.value;
						var cssClass = form.cssClass.value;
						$http({
							url : '/menu/add',
							params:{code:code, name:name, parent_id:parent_id , type:type, url:url, seq:seq, cssClass:cssClass},
							method : 'GET'
						}).success(function(data, header, config, status) {
							// 响应成功
							if(data=="1"){
								$('#addSuccessAlert').fadeIn(2000);
								$('#addSuccessAlert').fadeOut(2000);
								table.draw();
							}else{
								$('#addFailedAlert').fadeIn(2000);
								$('#addFailedAlert').fadeOut(2000);
							}
						}).error(function(data, header, config, status) {
							// 处理响应失败
							$('#addFailedAlert').fadeIn(2000);
							$('#addFailedAlert').fadeOut(2000);
						});
					};
					$scope.del_init=function(){
						var menuIds = "";
						$('[name=items]:checkbox:checked').each(function() {
							menuIds += $(this).val() + ";";
						});
						if(menuIds==""){
							$('#SelAlert').html("<strong>请选择记录进行删除！</strong>");
							$('#alertModal').modal("toggle");
						}else{
							$("#myModal3").modal("toggle");
						}
						
					}
					$scope.del = function(){
						var menuIds = "";
						$('[name=items]:checkbox:checked').each(function() {
							menuIds += $(this).val() + ";";
						});
						if(menuIds==""){
							alert("请选择要删除的记录！");
						}else{
							$http({
								url : '/menu/delete',
								params:{menuIds:menuIds},
								method : 'GET'
							}).success(function(data, header, config, status) {
								// 响应成功
								if(data==true){
									$('#promptContent').hide();
									$('#del').hide();
									$('#deleteSuccessAlert').fadeIn(2000);
									$('#myModal3').on('hide.bs.modal', function () {
										table.draw();
										$('#del').show();
										$('#deleteSuccessAlert').hide();
										$('#promptContent').show();
									});
								}else{
									$('#promptContent').hide();
									$('#del').hide();
									$('#deleteFailedAlert').fadeIn(2000);
									$('#myModal3').on('hide.bs.modal', function () {
										table.draw();
										$('#del').show();
										$('#deleteFailedAlert').hide();
										$('#promptContent').show();
									});
								}
								
							}).error(function(data, header, config, status) {
								// 处理响应失败
								$('#promptContent').hide();
								$('#del').hide();
								$('#deleteFailedAlert').fadeIn(2000);
								$('#myModal3').on('hide.bs.modal', function () {
									$('#del').show();
									$('#deleteFailedAlert').hide();
									$('#promptContent').show();
								});
							});
						}
						
					};
					$scope.update_init =function(){
						var menuIds = "";
						$('[name=items]:checkbox:checked').each(function() {
							menuIds += $(this).val() + ";";
						});
						if(menuIds==""){
							$('#SelAlert').html("<strong>请选择要修改的记录！</strong>");
							$('#alertModal').modal("toggle");
						}else if(menuIds.split(";").length>2){
							$('#SelAlert').html("<strong>修改的记录数不能大于1.</strong>");
							$('#alertModal').modal("toggle");
						}else{
							  
							 $("table :checkbox").each(function(key,value){
						            if($(value).prop('checked')){
						            	var selRow =$('#menu_table').dataTable().fnGetData(key-1);
						            	$scope.menu_id_update = selRow.MENU_ID;
						            	$scope.code_update = selRow.CODE;
						            	$scope.name_update = selRow.NAME;
						            	$scope.parent_id_update = selRow.PARENT_ID;
						            	$scope.parent_name_update = selRow.PARENT_NAME;
						            	$scope.type_update = selRow.TYPE;
						            	$scope.url_update = selRow.URL;
						            	$scope.seq_update =selRow.SEQ;
						            	$scope.cssClass_update =selRow.CSSCLASS;
						            }
							 });
							 $("#myModal2").modal("toggle");
						}
					};
					$scope.update = function(){
						var form = document.getElementById("updateForm");
						var menuId = form.menu_id_update.value;
						var code = form.code_update.value;
						var name = form.name_update.value;
						var parent_id = form.parent_id_update.value;
						var type = form.type_update.value;
						var url = form.url_update.value;
						var seq = form.seq_update.value;
						var cssClass = form.cssClass_update.value;
						$http({
							url : '/menu/edit',
							params:{menuId:menuId, code:code, name:name, parent_id:parent_id , type:type, url:url, seq:seq, cssClass:cssClass},
							method : 'GET'
						}).success(function(data, header, config, status) {
							// 响应成功
							if(data=="1"){
								$('#updateSuccessAlert').fadeIn(2000);
								$('#updateSuccessAlert').fadeOut(2000);
								table.draw();
							}else{
								$('#updateFailedAlert').fadeIn(2000);
								$('#updateFailedAlert').fadeOut(2000);
							}
						}).error(function(data, header, config, status) {
							// 处理响应失败
							$('#updateFailedAlert').fadeIn(2000);
							$('#updateFailedAlert').fadeOut(2000);
						});
					};
					
					$scope.query = function () {
						var url = '/menu/queryMenu?test=test';
						var form =document.getElementById("queryForm");  // $('#queryForm');
						if(form.menu_code_query.value!=0){
							url = url + '&menu_code=' + form.menu_code_query.value;
						}
						if(form.menu_name_query.value!=0){
							url = url + '&menu_name=' + form.menu_name_query.value;
						}
						if(form.url_query.value!=0){
							url = url + '&url=' + form.url_query.value;
						}
						if(form.parent_menu_name_query.value!=0){
							url = url + '&parent_menu_name=' + form.parent_menu_name_query.value;
						}
						
						$('#menu_table').dataTable().api().destroy();
						dataTableconfig.ajax=url;
						DataTables=$('#menu_table').dataTable(dataTableconfig);
					};
				});